package org.blogwu.learn.multithread.chapter3.demo18.v1;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainV2 extends Thread {

    /**
     * 这样不会出现线程安全问题了
     */
    ThreadLocal<SimpleDateFormat> tl = new ThreadLocal<>();

    @Override
    public void run() {
        try {
            if (tl.get() == null) {
                tl.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            }
            Date d = tl.get().parse("2018-10-10 22:20:53");
            System.out.println(d);
            tl.remove();//使用完记得remove掉哦
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        ExecutorService executors = Executors.newFixedThreadPool(1000);
        for (int i = 0; i < 1000; i++) {
            executors.submit(new MainV2());
        }
        executors.shutdown();
    }
}
